/*******************************************************************************
Copyright (C) 2021 Mestrelab Research S.L. All rights reserved.

This file is part of the MNova scripting toolkit.

Authorized users of MNova Software may use this file freely, but this file is
provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE WARRANTY OF DESIGN,
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*******************************************************************************/

/*jslint nomen: true, indent: 4, white: true, bitwise: true*/
/*globals Dir, FileInfo, Zip*/

//returns the number of files added to the zip
Zip.prototype.addDirectory = function (aPath) {
	"use strict";

	function addFiles(aDir, aFileList)
	{
		aDir.entryList("*", Dir.All | Dir.NoDotAndDotDot).forEach(function (aFileName) {
			var thisFi = new FileInfo(aDir.filePath(aFileName));
			if (thisFi.isDir) {
				addFiles(new Dir(thisFi.absoluteFilePath), aFileList);
			} else {
				aFileList.push(thisFi.absoluteFilePath);
			}
		});
		return aFileList;
	}

	var pathFi = new FileInfo(aPath), thisDir, files;

	if (!pathFi.isDir) {
		return -1;
	}

	thisDir = new Dir(aPath);
	files = addFiles(thisDir, []);
	files.forEach(function (aPath) {
		this.addFile(aPath);
	}, this);
	return files.length;
};
